package com.example.administrator.androidros.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.activity.LoginActivity;
import com.example.administrator.androidros.net.RequestCallback;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AppApplication;
import com.example.administrator.androidros.widget.CustomDialog;
import com.example.administrator.androidros.widget.CustomTipDialog;
import com.umeng.message.PushAgent;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity implements RequestCallback {
    protected BaseActivity mBaseActivity = null;
    protected CustomTipDialog tipDialog;//加载对话框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBaseActivity = this;
        setContentView();
        // 友盟配置
        PushAgent.getInstance(this).onAppStart();
        ButterKnife.bind(this);
        AppApplication.add(this);
        initData();
    }

    /**
     * 设置布局
     */
    protected abstract void setContentView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 页面跳转
     */
    protected void startOtherActivity(Class<?> className, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, className);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected void startOtherActivityForResult(Class<?> className, Bundle bundle,
                                               int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, className);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 弹出消息
     */
    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int msgID) {
        Toast.makeText(this, msgID, Toast.LENGTH_SHORT).show();
    }

    /**
     * 弹出对话框
     */
    protected void showInformationDialog(String message, int which1, int which2,
                                         DialogInterface.OnClickListener onClickListener) {
        new CustomDialog.Builder(this)
                .setTitle("智能家居")
                .setMessage(message)
                .setButton(which1, "确定", onClickListener)
                .setButton(which2, "取消", onClickListener)
                .setCancelable(false)
                .create()
                .show();
    }

    /**
     * 弹出提示对话框
     * canceable: back键可以取消：true
     * 不可以取消：false
     */
    protected void startTipDialog(int iconType, String message, boolean cancelable) {
        if (tipDialog == null) {
            tipDialog = new CustomTipDialog(this);
            tipDialog.setIconType(iconType);
            tipDialog.setMessage(message);
            tipDialog.setCanceledOnTouchOutside(false);
            tipDialog.setCancelable(cancelable);
        }
        tipDialog.show();
    }

    /**
     * 销毁加载对话框
     */
    protected void stopTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
        }
        tipDialog = null;
    }

    //公共标题处理
    protected void configCommonHeader(final Activity activity, String title, boolean visibility) {
        RelativeLayout header = (RelativeLayout) activity.findViewById(R.id.common_head);
        TextView titleTV = (TextView) header.findViewById(R.id.common_head_title);
        titleTV.setText(title);
        ImageView back = (ImageView) findViewById(R.id.common_head_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ImageView more = (ImageView) header.findViewById(R.id.common_head_more);
        if (visibility) {
            more.setVisibility(View.VISIBLE);
        } else {
            more.setVisibility(View.GONE);
        }
    }

    protected abstract void onFailure(int requestCode, Object object);

    protected abstract void onSuccess(int requestCode, Object object);

    @Override
    public void onSuccessCallback(int requestCode, Object object) {
        onSuccess(requestCode, object);
    }

    @Override
    public void onFailureCallback(int requestCode, Object object) {
        onFailure(requestCode, object);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        AppApplication.remove(this);
        stopTipDialog();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (tipDialog != null)
            tipDialog = null;
    }

    public void exit() {
        Intent intent = new Intent();
        intent.setClass(mBaseActivity, LoginActivity.class);
        intent.setAction(AgentConstants.Action.EXIT_APP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    public void hideSoftKeyboard(EditText editText, Context context) {
        if (editText != null && context != null) {
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }
}
